package weaver.formmode.customjavacode.modeexpand.srm.util;

import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.formmode.setup.ModeRightInfo;
import weaver.general.BaseBean;
import weaver.general.Util;

/**
 * @Title: Ecology9
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2021-03-23
 * @Description: ${description}
 * @ModifyLog:
 **/

public class SrmUtil {
    //判断采购类型转换成名称
    public static String getCglx2Name(String cglx) {
        String cglxName = "";
        switch (cglx) {
            case "0":
                cglxName = "WL";
                break;
            case "1":
                cglxName = "GC";
                break;
            case "2":
                cglxName = "ZL";
                break;
            case "3":
                cglxName = "YS";
                break;
            case "4":
                cglxName = "FW";
                break;
        }
        return cglxName;
    }

    //生成任务编码
    public static String generTaskCode(String bmlx) {
        RecordSet rs = new RecordSet();
        String newdqlsh = "1";
        String newdqbm = "";
        String currentDate = DateUtil.getCurrentDate();
        String curDate = currentDate.substring( 4, 10 );
        String curYear = currentDate.substring( 0, 4 );
        if (curDate.equals( "-01-01" )) {
            newdqlsh = autoGenericCode( newdqlsh, 6 );
            String bmlxName = getCglx2Name( bmlx );
            newdqbm = "CGRW-" + bmlxName + "-" + curYear + "-" + newdqlsh;
            String sql1 = "update uf_serial_number set dqrq=?,dqlsh=?,dqbm=?";
            rs.executeUpdate( sql1, new Object[]{currentDate, newdqlsh, newdqbm} );
        } else {
            String sql = "select step,dqlsh from uf_serial_number where bmlx=?";
            rs.executeQuery( sql, bmlx );
            if (rs.next()) {
                int step = Util.getIntValue( rs.getString( "step" ) );//步值
                int dqlsh = Util.getIntValue( rs.getString( "dqlsh" ) );//当前流水号
                newdqlsh = autoGenericCode( Util.getNumStr( dqlsh + step ), 6 );
                String bmlxName = getCglx2Name( bmlx );
                newdqbm = "CGRW-" + bmlxName + "-" + curYear + newdqlsh;
                String sql1 = "update uf_serial_number set dqrq=?,dqlsh=?,dqbm=? where bmlx=?";
                rs.executeUpdate( sql1, new Object[]{currentDate, newdqlsh, newdqbm, bmlx} );
            } else {
                newdqlsh = autoGenericCode( newdqlsh, 6 );
                String bmlxName = getCglx2Name( bmlx );
                newdqbm = "CGRW-" + bmlxName + "-" + curYear + newdqlsh;
                String sql1 = "insert into uf_serial_number (dqrq,step,dqlsh,bmlx,dqbm) values (?,?,?,?,?)";
                rs.executeUpdate( sql1, new Object[]{currentDate, "1", newdqlsh, bmlx, newdqbm} );
            }
        }

        return newdqbm;
    }

    //不够位数的在前面补0，保留num的长度位数字
    public static String autoGenericCode(String code, int num) {
        String result = "";
        result = String.format( "%0" + num + "d", Integer.parseInt( code ) );
        return result;
    }

    //获取采购需求明细未勾选的条数
    public static boolean getDtlCounts(String mainid, String cglx) {
        RecordSet rs = new RecordSet();
        boolean flag = true;
        String sql = "select count(id) as counts from " + getCgxqTableType( cglx ) + "_dt1 where mainid=? and nvl(cfbj,0)=0";
        rs.executeQuery( sql, mainid );
        rs.next();
        int counts = Util.getIntValue( rs.getString( "counts" ) );
        if (counts > 0) {
            flag = false;
        }
        return flag;
    }

    //根据采购需求类型获取采购需求表单
    public static String getCgxqTableType(String cglx) {
        String table = "";
        BaseBean baseBean = new BaseBean();
        switch (cglx) {
            case "0":
                table = baseBean.getPropValue( "SrmMdTab", "cgxqwl" );
                break;
            case "1":
                table = baseBean.getPropValue( "SrmMdTab", "cgxqgc" );
                break;
            case "2":
                table = baseBean.getPropValue( "SrmMdTab", "cgxqzl" );
                break;
            case "3":
                table = baseBean.getPropValue( "SrmMdTab", "cgxqys" );
                break;
            case "4":
                table = baseBean.getPropValue( "SrmMdTab", "cgxqfw" );
                break;
        }
        return table;
    }

    //根据采购需求类型获取需求拆分表单
    public static String getXqcfTableType(String cglx) {
        String table = "";
        BaseBean baseBean = new BaseBean();
        switch (cglx) {
            case "0":
                table = baseBean.getPropValue( "SrmMdTab", "xqcfwl" );
                break;
            case "1":
                table = baseBean.getPropValue( "SrmMdTab", "xqcfgc" );
                break;
            case "2":
                table = baseBean.getPropValue( "SrmMdTab", "xqcfzl" );
                break;
            case "3":
                table = baseBean.getPropValue( "SrmMdTab", "xqcfys" );
                break;
            case "4":
                table = baseBean.getPropValue( "SrmMdTab", "xqcffw" );
                break;
        }
        return table;
    }

    //根据采购需求类型获取任务合并表单
    public static String getRwhbTableType(String cglx) {
        String table = "";
        BaseBean baseBean = new BaseBean();
        switch (cglx) {
            case "0":
                table = baseBean.getPropValue( "SrmMdTab", "rwhbwl" );
                break;
            case "1":
                table = baseBean.getPropValue( "SrmMdTab", "rwhbgc" );
                break;
            case "2":
                table = baseBean.getPropValue( "SrmMdTab", "rwhbzl" );
                break;
            case "3":
                table = baseBean.getPropValue( "SrmMdTab", "rwhbys" );
                break;
            case "4":
                table = baseBean.getPropValue( "SrmMdTab", "rwhbfw" );
                break;
        }
        return table;
    }

    //根据采购任务类型获取采购任务表单
    public static String getCgrwTableType(String cglx) {
        String table = "";
        BaseBean baseBean = new BaseBean();
        switch (cglx) {
            case "0":
                table = baseBean.getPropValue( "SrmMdTab", "cgrwwl" );
                break;
            case "1":
                table = baseBean.getPropValue( "SrmMdTab", "cgrwgc" );
                break;
            case "2":
                table = baseBean.getPropValue( "SrmMdTab", "cgrwzl" );
                break;
            case "3":
                table = baseBean.getPropValue( "SrmMdTab", "cgrwys" );
                break;
            case "4":
                table = baseBean.getPropValue( "SrmMdTab", "cgrwfw" );
                break;
        }
        return table;
    }

    //获取插入数据id
    public static String getRwbh2Id(String rwbh, String cglx) {
        RecordSet rs = new RecordSet();
        String sql = "select id from " + getCgrwTableType(cglx) + " where rwbh=?";
        rs.executeQuery( sql, rwbh );
        rs.next();
        String id = Util.null2String( rs.getString( "id" ) );
        return id;
    }

    //获取formmodeid
    public static String getFormmodeId(String cglx) {
        String tableName = getCgrwTableType(cglx);
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select m.id from modeinfo m left join WORKFLOW_BILL b on m.formid = b.id where b.tablename = ?", tableName );
        rs.next();
        String formmodeid = Util.null2String( rs.getString( "id" ) );
        return formmodeid;
    }
    //获取根据任务单ID获取任务池Id
    public static String getRwdmxid(String ids){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select wm_concat(id) as rwmxids from uf_cgrw_wl_dt1 where mainid  in ("+ids+")");
        rs.next();
        String rwmxids = Util.null2String(rs.getString( "rwmxids" ));
        return rwmxids;
    }
    //合计预计金额
    public static String getTotalAmount(String rwdid,String lcid,String cgrwTable,String formtable){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select nvl(sum(ygje),0) from "+cgrwTable+" where id in ("+rwdid+")");
        rs.next();
        String rwd_ygje = Util.null2String(rs.getString(1));
        rs.executeQuery("select nvl(ygjeY,0) from "+formtable+" where requestid="+lcid+"");
        rs.next();
        String lc_ygje = Util.null2String(rs.getString(1));
        String sumygje = CalUtil.fAdd(rwd_ygje,lc_ygje);
        rs.executeUpdate("update "+formtable+" set ygjeY="+sumygje+" where requestid="+lcid+"");
        rs.writeLog( "sumygje="+sumygje );
        return sumygje;
    }
    //流程表单根据requestid获取ID
    public static String getRequestid2Id(String requestid,String formtable) {
        RecordSet rs = new RecordSet();
        rs.executeQuery( "select id from "+formtable+" where requestid=?", requestid );
        rs.next();
        String id = Util.null2String( rs.getString( "id" ) );
        return id;
    }
    //建模权限重构
    public static void getNewModeRight(String userid, String formmodeid, String id) {
        ModeRightInfo modeRightInfo = new ModeRightInfo();
        modeRightInfo.setNewRight( true );
        modeRightInfo.editModeDataShare( weaver.general.Util.getIntValue( userid ), weaver.general.Util.getIntValue( formmodeid ), weaver.general.Util.getIntValue( id ) );
    }
}
